package com.pm25.request;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.pm25.common.Constants;
import com.pm25.exception.ValidateException;
import com.pm25.response.Pm25Response;
import com.pm25.response.bean.Pm25;

public class Pm25Request implements Request<Pm25Response> {
	protected static Gson gson = new GsonBuilder().setDateFormat(Constants.GSON_DATE_PATTERN).create();

	private String city;
	private Boolean avg;
	private String stations;

	public String getCity() {
		return city;
	}

	/**
	 * 
	 * @Title setCity
	 * @param city
	 *            城市名称，必选参数
	 */
	public void setCity(String city) {
		this.city = city;
	}

	public Boolean getAvg() {
		return avg;
	}

	/**
	 * 
	 * @Title setAvg
	 * @param avg
	 *            是否返回一个城市所有监测点数据均值的标识，可选参数，默认是true，不需要均值时传这个参数并设置为false
	 */
	public void setAvg(Boolean avg) {
		this.avg = avg;
	}

	public String getStations() {
		return stations;
	}

	/**
	 * 
	 * @Title setStations
	 * @param stations
	 *            是否只返回一个城市均值的标识，可选参数，默认是yes，不需要监测点信息时传这个参数并设置为no
	 */
	public void setStations(String stations) {
		this.stations = stations;
	}

	public void valid() throws ValidateException {
		if (StringUtils.isBlank(city)) {
			throw new ValidateException("城市名称不能为空");
		}
	}

	public Map<String, String> getParamterMaps() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("city", city);
		param.put("avg", String.valueOf(avg));
		param.put("stations", stations);
		return param;
	}

	public String getUri() {
		return "/api/querys/pm2_5.json";
	}

	public Class<Pm25Response> getResponseClass() {
		return Pm25Response.class;
	}

	public static void main(String[] args) {
		String s = "[{\"aqi\":57,\"area\":\"重庆\",\"pm2_5\":17,\"pm2_5_24h\":18,\"position_name\":\"缙云山\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1414A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":108,\"area\":\"重庆\",\"pm2_5\":27,\"pm2_5_24h\":32,\"position_name\":\"高家花园\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"轻度污染\",\"station_code\":\"1415A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":46,\"area\":\"重庆\",\"pm2_5\":15,\"pm2_5_24h\":24,\"position_name\":\"天生\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1416A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":45,\"area\":\"重庆\",\"pm2_5\":21,\"pm2_5_24h\":34,\"position_name\":\"两路\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1417A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":45,\"area\":\"重庆\",\"pm2_5\":15,\"pm2_5_24h\":31,\"position_name\":\"虎溪\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1418A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":48,\"area\":\"重庆\",\"pm2_5\":33,\"pm2_5_24h\":36,\"position_name\":\"南坪\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1419A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":60,\"area\":\"重庆\",\"pm2_5\":19,\"pm2_5_24h\":25,\"position_name\":\"唐家沱\",\"primary_pollutant\":\"颗粒物(PM10)\",\"quality\":\"良\",\"station_code\":\"1420A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":67,\"area\":\"重庆\",\"pm2_5\":24,\"pm2_5_24h\":25,\"position_name\":\"茶园\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1421A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":85,\"area\":\"重庆\",\"pm2_5\":36,\"pm2_5_24h\":47,\"position_name\":\"白市驿\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1422A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":88,\"area\":\"重庆\",\"pm2_5\":23,\"pm2_5_24h\":40,\"position_name\":\"解放碑\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1423A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":67,\"area\":\"重庆\",\"pm2_5\":24,\"pm2_5_24h\":32,\"position_name\":\"杨家坪\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1424A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":45,\"area\":\"重庆\",\"pm2_5\":26,\"pm2_5_24h\":27,\"position_name\":\"空港\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1425A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":45,\"area\":\"重庆\",\"pm2_5\":20,\"pm2_5_24h\":35,\"position_name\":\"新山村\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1426A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":108,\"area\":\"重庆\",\"pm2_5\":30,\"pm2_5_24h\":37,\"position_name\":\"礼嘉\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"轻度污染\",\"station_code\":\"1427A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":53,\"area\":\"重庆\",\"pm2_5\":28,\"pm2_5_24h\":28,\"position_name\":\"蔡家\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1428A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":41,\"area\":\"重庆\",\"pm2_5\":26,\"pm2_5_24h\":30,\"position_name\":\"鱼新街\",\"primary_pollutant\":null,\"quality\":\"优\",\"station_code\":\"1429A\",\"time_point\":\"2016-07-04T17:00:00Z\"},{\"aqi\":83,\"area\":\"重庆\",\"pm2_5\":36,\"pm2_5_24h\":33,\"position_name\":\"南泉\",\"primary_pollutant\":\"臭氧1小时\",\"quality\":\"良\",\"station_code\":\"1430A\",\"time_point\":\"2016-07-04T17:00:00Z\"}]";
		Type type = new TypeToken<ArrayList<Pm25>>() {
		}.getType();
		ArrayList<Pm25> pm25 = gson.fromJson(s, type);
		System.out.println(pm25.get(0).getTimePoint());
		
		try {
			System.out.println(DateUtils.parseDate("2016-07-04T17:00:00Z", "yyyy-MM-dd'T'HH:mm:ss'Z'"));
			;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
